//
//  ReloadControlTableViewCell.m
//  MKUI
//
//  Created by 苏尚进 on 2020/11/10.
//Copyright © 2020 moon. All rights reserved.
//

#import "ReloadControlTableViewCell.h"

@interface ReloadControlTableViewCell ()
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end

@implementation ReloadControlTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
	if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        NSMutableArray *buttons = [[NSMutableArray alloc] init];
        for (int i=0; i<10; i++) {
            MKUIFlowLayoutButton *b = [[MKUIFlowLayoutButton alloc] init];
            [b setTitleColor:[UIColor systemBlueColor] forState:UIControlStateNormal];
            b.contentInsets = MKUIEdgeInsetsMakeSameEdge(5);
            b.titleLabel.font = [UIFont systemFontOfSize:12];
            [b setTitle:[@(i) description] forState:UIControlStateNormal];
            [b addTarget:self action:@selector(__onButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:b];
            [buttons addObject:b];
        }
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:buttons constraintParam:(MKUIFlowLayoutConstraintParam_H_B_L) contentInsets:MKUIEdgeInsetsMakeSameEdge(5) interitemSpacing:5];
	}
	return self;
}
- (void)__onButtonDidTap:(MKUIFlowLayoutButton *)button{
    NSInteger i = [self.flowlayout.items indexOfObject:button];
    NSIndexPath *p = [NSIndexPath indexPathForRow:0 inSection:i];
    MKUITableViewSectionModel *sm = [self.cellModel.tableViewModel sectionModelAtIndex:i];
    MKUITableViewCellModel *cm = [self.cellModel.tableViewModel cellModelAtIndexPath:p];
    MKUITableViewCellModel *cm2 = [MKUITableViewCellModel modelWithValue:cm.modelValue];
    cm2.cellClass = cm.cellClass;
    MKUIFlowLayoutButtonContentStyle contentStyle = [cm mk_integerForKeyPath:@"contentStyle" otherwise:MKUIFlowLayoutButtonContentStyleHorizontal];
    cm2[@"contentStyle"] = @((contentStyle+1)%2);
    [sm removeAllCellModels];
    [sm addCellModel:cm2];
//    UITableView *tableView = self.mk_tableViewOfFirst;
//    [tableView beginUpdates];
//    [tableView deleteRowsAtIndexPaths:@[p] withRowAnimation:(UITableViewRowAnimationNone)];
//    [tableView insertRowsAtIndexPaths:@[cm2.indexPathInModel] withRowAnimation:UITableViewRowAnimationAutomatic];
//    [tableView endUpdates];
    [sm refreshWithAnimated:NO];
//    [self.mk_tableViewOfFirst reloadData];
}
- (void)customLayoutSubviews{
	[super customLayoutSubviews];
	CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
//	___VARIABLE_modelClassName:identifier___ *modelValue = self.cellModel.modelValue;
}
//- (void)tableView:(UITableView *)tableView willDisplayCellModel:(__kindof MKUITableViewCellModel *)cellModel{
//    
//}
//- (void)tableView:(UITableView *)tableView didEndDisplayingCellModel:(__kindof MKUITableViewCellModel *)cellModel{
//
//}

- (CGSize)customSizeThatFits:(CGSize)size{
	return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
@end
